home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-06 | 1.7 KB | 46 lines | [TEXT/GEOL] |
- Item forwarded by THOUGHT.SHOP to MADA.LIB
-
- Item 3772254 10-Oct-90 14:03PDT
-
- From: DEREK White, Derek
-
- To: CPLUS.DEV$ C++ Interest List--Developers
- CPLUS.APPLE$ C++ Interest List--Apple Employees
-
- Sub: How do I use virtual op== ?
-
- Is there a way to make operator== work polymorphically and "correctly"? If
- I have references to two objects, then I would like the most specific op== if
- the operands are the same type. Here's an example (assuming class TShape is
- the base class for TRect):
-
- foo(TShape& a, TShape& b) {
- if (a == b)
- ; // beep, whatever...
- }
-
- main {
- TShape aShape;
- TRect aRect;
-
- foo(aShape, aShape); // should call TShape::op==(TShape&)
- foo(aShape, aRect); // should call TShape::op==(TShape&) ?
- foo(aRect, aShape); // should call TShape::op==(TShape&) ?
- foo(aRect, aRect); // should call TRect::op==(TRect&)<-different type!
- }
-
- The point I ran across here is that if TShape::op==(TShape&) is virtual,
- then TRect has to declare TRect::op==(TShape&). But what's the point of
- comparing TRects and TShapes? The base class's op== will work just fine unless
- the parameter to TRect::op==(TShape&) really is a TRect, but from inside
- TRect::op== how can you tell?
- The other point is that I tend to think of equality as a symmetric
- operation. (a == b) should equal (b == a).
- This isn't very clear, the example isn't realistic, and the syntax is
- probably messed up, but I hope somebody gets the idea. Is there a way to make
- this work, or should I forget virtual op==? (Maybe virtual symmetric
- operators}
-
- Derek White
-
-